package com.server.common.seata;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * seata 请求拦截器
 * 
 */
@Component
@Slf4j
@ConditionalOnClass({RequestInterceptor.class, GlobalTransactional.class})
public class SeataRequestInterceptor implements RequestInterceptor
{

    @Override
    public void apply(RequestTemplate requestTemplate)
    {
        String xid = RootContext.getXID();
        if (!ObjectUtils.isEmpty(xid)) {
            requestTemplate.header(RootContext.KEY_XID, xid);
        }

    }

}